/*
 * UserControler.java
 *
 * Created on 27 novembre 2007, 15:10
 */
package com.reimscopie.controlers ;

import javax.naming.* ;

import com.reimscopie.* ;
import com.reimscopie.beans.interfaces.* ;
import java.util.* ;


/**
 * This class is a JSF managed bean.
 */
public class UserControler extends WebControler {
  
  // the user bean
  private ReimsCopieUserRemote bean ;
  
  
  /**
   * Create a new <code>UserControler</code>.
   */
  public UserControler() throws NamingException {
    
    String name ="com.reimscopie.beans.session.ReimsCopieUserBean_" +
        ReimsCopieUserRemote.class.getName()+"@Remote" ;
    
    bean = (ReimsCopieUserRemote)getContext().lookup(name) ;
    
  }
  
  
  // login
  private String    login ;
  public void      setLogin(String login)       {this.login = login;}
  public String    getLogin()                   {return login;}
  
  // password
  private String   password ;
  public void      setPassword(String password) {this.password = password;}
  public String    getPassword()                {return password;}
  
  // connexionstate
  public boolean   getConnexionstate()          {return bean.isLogged();}
  
  // language
  private String   language;
  public void      setLanguage(String language) {this.language = language;}
  public String    getLanguage()                {return language;}
  
  // about purchasing
  private long productId ;
  public void setProductId(long productId)      {this.productId = productId;}
  
  private int quantity ;
  public void setQuantity(int quantity)        {this.quantity = quantity;}
  public int getQuantity()                     {return 1;}
  
  // about comments
  private String comment ;
  public void setComment(String comment)       {this.comment = comment;}
  public String getComment()                   {return "Votre commentaire";}
  
  //about errors
  private String feedback = "Money";
  public void setFeedback(String feedback)           {this.feedback = feedback;}
  public String getFeedback()                        {return feedback;}
  
  
  /**
   * This method is invoked when the user try to log in.
   *
   * <p>Navigation keys are: 
   *  - login.accepted
   *  - login.rejected
   *  - error
   */
  public String login() throws Exception {
    
    beginTransaction() ;
    
    String key ;
    try {
      if (bean.login(login,password))
        key = "login.accepted" ;
      else{
        key = "feedback" ;
        setFeedback("Badlogin") ;
      }
    }
    catch (Exception e) {
      rollbackTransaction() ;
      setFeedback("Badlogin") ;
      return "feedback" ;
    }
    
    commitTransaction() ;
    return key ;
  }
  
  
  /**
   * This method is invoked when the user logout.
   *
   * <p>Navigation keys are:
   * - home
   */
  public String logout() throws Exception {
    
    bean.logout() ;
    
    password = null ;
    
    return "home" ;
    
  }
  
  
  /**
   * Transform the current cart into a new order.
   *
   * <p>Navigation keys are:
   * - home
   * - feedback
   */
  public String orderCart() throws Exception {
    
    beginTransaction() ;
    
    try {
      boolean ok = bean.orderCart() ;
      commitTransaction() ;
      if(ok){
        setFeedback("OrderOK") ;
        return "feedback" ;
      }
      else{
        setFeedback("Money") ;
        return "feedback" ;
      }
    }
    catch (Exception e) {
      rollbackTransaction() ;
      setFeedback("Unknown") ;
      return "feedback" ;
    }
  }
  
  
  /**
   * Empty all products from the cart.
   *
   * <p>Navigation keys are:
   * - cart
   * - error
   */
  public String emptyCart() throws Exception {
    
    beginTransaction() ;
    
    try {
      bean.emptyCart() ;
      commitTransaction() ;
      return "cart" ;
    }
    catch (Exception e) {
      e.printStackTrace() ;
      rollbackTransaction() ;
      setFeedback("Unknown") ;
      return "feedback" ;
    }
    
  }
  
  
  /**
   * Add a purchase in the cart
   *
   * @param purchase The purchase to add in the cart.
   */
  public void addPurchase() throws Exception {
      
    beginTransaction() ;
    
    try {
      Product product = bean.getProduct(productId) ;
      Purchase purchase = new Purchase(product,quantity) ;
      bean.addPurchase(purchase) ;
      commitTransaction() ;
    }
    catch (Exception e) {
      e.printStackTrace();
      rollbackTransaction() ;
    }
  }
  
  
  /**
   * Update the user account.
   *
   * @param a The new account value.
   */
  public void updateUserAccount(Account a) {
    bean.updateUserAccount(a) ;
  }
  
 public String addComment()throws Exception {
    Date date = new Date() ;
    if(!bean.isLogged()){
        setFeedback("Notlogged") ;
        return "feedback" ;
    }
        
    Comment com = new Comment(comment,date,getAccount()) ;
        try {
            beginTransaction() ;
            bean.createComment(com,(long)productId) ;
        } catch (java.lang.Exception e) {
            e.printStackTrace();
            rollbackTransaction() ;
        }
    commitTransaction();
    setFeedback("Comment") ;
    return "feedback" ;
 }
  
////////////////////////////////////////////////////////////////////////////////
//                             ACCOUNT ACCESS                                 //
////////////////////////////////////////////////////////////////////////////////
  
  /**
   * Return the user account.
   *
   * @return The user account, or <code>null</code> if the user is not logged
   * in.
   */
  public Account getAccount() {
    if (bean == null)
      return null ;
    else
      return bean.getUserAccount() ;
  }
  
  
  /**
   * Return the balance of the user account.
   *
   * @return The user phone, or <code>0</code> if the user is not logged
   * in.
   */
  public float getBalance() {
    
    Account account = bean.getUserAccount() ;
    if (account == null)
      return 0;
    else {
      return account.getBalance() ;
    }  
      
  }
  
  
  /**
   * Return the content of the cart.
   *
   * @return The content of the cart.
   */
  public Purchase[] getCart() {
    return bean.getCart() ;
  }
  
}
